package com.hildeberto.home.client;

import com.conseilimmo.bo.util.ConstantesConseilImmo;
import com.conseilimmo.bo.util.Convert;
import com.conseilimmo.repository.BatimentBsn;
import com.conseilimmo.repository.LocataireBsn;
import com.conseilimmo.repository.LocationBsn;
import com.hildeberto.home.util.EntityManagerHelper;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javax.persistence.EntityManager;
import objetsBd.BatimentBd;
import objetsBd.LocataireBd;
import objetsBd.LocationBd;

/**
 *
 * @author verpoorten
 */
public class BatimentController implements Initializable {

    private static final Logger LOGGER = Logger.getLogger(BatimentController.class.getName());

    @FXML
    private Label batimentSelectionne;
    private Stage thisStage;

    @FXML
    private TextField batimentTxtDenomination;
    @FXML
    private TextField batimentTxtAdrRue;
    @FXML
    private TextField batimentTxtAdrCP;
    @FXML
    private TextField batimentTxtAdrLocalite;
    @FXML
    private TextField batimentTxtSurface;
    @FXML
    private TextField batimentTxtPEB;
    @FXML
    private TextField batimentTxtDescription;
    @FXML
    private TextField batimentTxtLoyer;
    @FXML
    private TextField batimentTxtCharges;
    @FXML
    private TextField batimentTxtPrecompte;
    @FXML
    private TextField batimentTxtCreditBanque;
    @FXML
    private TextField batimentTxtCreditMntTot;
    @FXML
    private TextField batimentTxtCreditAchat;
    @FXML
    private TextField batimentTxtCreditTravaux;
    @FXML
    private TextField batimentTxtCreditMensualite;
    @FXML
    private TextField batimentTxtCreditDebut;
    @FXML
    private TextField batimentTxtCreditFin;
    @FXML
    private TextField batimentTxtAssuranceProprio;
    @FXML
    private TextField batimentTxtAssuranceLocataire;
    @FXML
    private Label batimentLblId;
    @FXML
    private Label batimentLblIdLocations;
    @FXML
    private TextArea messageInfo;
    private BatimentBd batimentEnCours;

    public BatimentController() {
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        LOGGER.log(Level.INFO, "initialize batiment controller");
        batimentTxtDenomination.setText("test");
        rechercherBatimentParId(1L);
    }

    private void rechercherBatimentParId(Long idBatiment) {
        batimentEnCours = null;
        BatimentBd batiment = new BatimentBd();
        if (!MainApp.getModeAvecBd()) {
            batiment.setDenomination("denom1");
            batiment.setAdresse("rue du centre 1");
        } else {
            EntityManager em = EntityManagerHelper.EM.open();
            BatimentBsn batrimentSn = new BatimentBsn();
            batiment = batrimentSn.trouverBatimentParId(idBatiment);

            EntityManagerHelper.EM.close();
        }
        changerDeBatiment(batiment);

    }

    public TextField getBatimentTxtDenomination() {
        return batimentTxtDenomination;
    }

    public void setBatimentTxtDenomination(TextField batimentTxtDenomination) {
        this.batimentTxtDenomination = batimentTxtDenomination;
    }

    public TextField getBatimentTxtAdrRue() {
        return batimentTxtAdrRue;
    }

    public void setBatimentTxtAdrRue(TextField batimentTxtAdrRue) {
        this.batimentTxtAdrRue = batimentTxtAdrRue;
    }

    public Label getBatimentSelectionne() {
        return batimentSelectionne;
    }

    public void setBatimentSelectionne(Label batimentSelectionne) {
        this.batimentSelectionne = batimentSelectionne;
    }

    public TextField getBatimentTxtAdrCP() {
        return batimentTxtAdrCP;
    }

    public void setBatimentTxtAdrCP(TextField batimentTxtAdrCP) {
        this.batimentTxtAdrCP = batimentTxtAdrCP;
    }

    public TextField getBatimentTxtAdrLocalite() {
        return batimentTxtAdrLocalite;
    }

    public void setBatimentTxtAdrLocalite(TextField batimentTxtAdrLocalite) {
        this.batimentTxtAdrLocalite = batimentTxtAdrLocalite;
    }

    public TextField getBatimentTxtSurface() {
        return batimentTxtSurface;
    }

    public void setBatimentTxtSurface(TextField batimentTxtSurface) {
        this.batimentTxtSurface = batimentTxtSurface;
    }

    public TextField getBatimentTxtPEB() {
        return batimentTxtPEB;
    }

    public void setBatimentTxtPEB(TextField batimentTxtPEB) {
        this.batimentTxtPEB = batimentTxtPEB;
    }

    public TextField getBatimentTxtDescription() {
        return batimentTxtDescription;
    }

    public void setBatimentTxtDescription(TextField batimentTxtDescription) {
        this.batimentTxtDescription = batimentTxtDescription;
    }

    public TextField getBatimentTxtLoyer() {
        return batimentTxtLoyer;
    }

    public void setBatimentTxtLoyer(TextField batimentTxtLoyer) {
        this.batimentTxtLoyer = batimentTxtLoyer;
    }

    public TextField getBatimentTxtCharges() {
        return batimentTxtCharges;
    }

    public void setBatimentTxtCharges(TextField batimentTxtCharges) {
        this.batimentTxtCharges = batimentTxtCharges;
    }

    public TextField getBatimentTxtPrecompte() {
        return batimentTxtPrecompte;
    }

    public void setBatimentTxtPrecompte(TextField batimentTxtPrecompte) {
        this.batimentTxtPrecompte = batimentTxtPrecompte;
    }

    public TextField getBatimentTxtCreditBanque() {
        return batimentTxtCreditBanque;
    }

    public void setBatimentTxtCreditBanque(TextField batimentTxtCreditBanque) {
        this.batimentTxtCreditBanque = batimentTxtCreditBanque;
    }

    public TextField getBatimentTxtCreditMntTot() {
        return batimentTxtCreditMntTot;
    }

    public void setBatimentTxtCreditMntTot(TextField batimentTxtCreditMntTot) {
        this.batimentTxtCreditMntTot = batimentTxtCreditMntTot;
    }

    public TextField getBatimentTxtCreditAchat() {
        return batimentTxtCreditAchat;
    }

    public void setBatimentTxtCreditAchat(TextField batimentTxtCreditAchat) {
        this.batimentTxtCreditAchat = batimentTxtCreditAchat;
    }

    public TextField getBatimentTxtCreditTravaux() {
        return batimentTxtCreditTravaux;
    }

    public void setBatimentTxtCreditTravaux(TextField batimentTxtCreditTravaux) {
        this.batimentTxtCreditTravaux = batimentTxtCreditTravaux;
    }

    public TextField getBatimentTxtCreditMensualite() {
        return batimentTxtCreditMensualite;
    }

    public void setBatimentTxtCreditMensualite(TextField batimentTxtCreditMensualite) {
        this.batimentTxtCreditMensualite = batimentTxtCreditMensualite;
    }

    public TextField getBatimentTxtCreditDebut() {
        return batimentTxtCreditDebut;
    }

    public void setBatimentTxtCreditDebut(TextField batimentTxtCreditDebut) {
        this.batimentTxtCreditDebut = batimentTxtCreditDebut;
    }

    public TextField getBatimentTxtCreditFin() {
        return batimentTxtCreditFin;
    }

    public void setBatimentTxtCreditFin(TextField batimentTxtCreditFin) {
        this.batimentTxtCreditFin = batimentTxtCreditFin;
    }

    public TextField getBatimentTxtAssuranceProprio() {
        return batimentTxtAssuranceProprio;
    }

    public void setBatimentTxtAssuranceProprio(TextField batimentTxtAssuranceProprio) {
        this.batimentTxtAssuranceProprio = batimentTxtAssuranceProprio;
    }

    public TextField getBatimentTxtAssuranceLocataire() {
        return batimentTxtAssuranceLocataire;
    }

    public void setBatimentTxtAssuranceLocataire(TextField batimentTxtAssuranceLocataire) {
        this.batimentTxtAssuranceLocataire = batimentTxtAssuranceLocataire;
    }

    public Label getBatimentLblId() {
        return batimentLblId;
    }

    public void setBatimentLblId(Label batimentLblId) {
        this.batimentLblId = batimentLblId;
    }

    public Label getBatimentLblIdLocations() {
        return batimentLblIdLocations;
    }

    public void setBatimentLblIdLocations(Label batimentLblIdLocations) {
        this.batimentLblIdLocations = batimentLblIdLocations;
    }

    public void setStage(Stage stage) {
        thisStage = stage;
    }

    public void showStage() {
        thisStage.setTitle("Titel in der MainController.java geändert");
        thisStage.show();
    }

    public TextArea getMessageInfo() {
        return messageInfo;
    }

    public void setMessageInfo(TextArea messageInfo) {
        this.messageInfo = messageInfo;
    }

    public void pEditionGarnirDetailBatiment(BatimentBd bat) {
        this.batimentTxtDenomination.setText(bat.getDenomination());

        batimentTxtAdrRue.setText(bat.getAdresseRue());
        batimentTxtAdrCP.setText(bat.getAdresseCP());
        batimentTxtAdrLocalite.setText(bat.getAdresseLocalite());
        batimentTxtPEB.setText(bat.getPEB());
        batimentTxtSurface.setText(Convert.convertDoubleToString(bat.getSurface()));

        batimentTxtDescription.setText(bat.getDescription());
        batimentTxtLoyer.setText(Convert.convertDoubleToString(bat.getLoyer()));
        batimentTxtCharges.setText(Convert.convertDoubleToString(bat.getCharge()));
        batimentTxtPrecompte.setText(Convert.convertDoubleToString(bat.getPrecompteImmobilier()));
        batimentTxtAssuranceProprio.setText(bat.getAssuranceProprio());
        batimentTxtAssuranceLocataire.setText(bat.getAssuranceLocataire());
        batimentTxtCreditBanque.setText(bat.getCreditBanque());
        batimentTxtCreditMntTot.setText(Convert.convertDoubleToString(bat.getCreditMontantTotal()));
        batimentTxtCreditAchat.setText(Convert.convertDoubleToString(bat.getCreditMontantAchat()));
        batimentTxtCreditTravaux.setText(Convert.convertDoubleToString(bat.getCreditMontantTravaux()));
        batimentTxtCreditMensualite.setText(Convert.convertDoubleToString(bat.getCreditMontantMensualite()));
        batimentTxtCreditDebut.setText(Convert.convertDateToString(bat.getCreditDateDebut()));
        batimentTxtCreditFin.setText(Convert.convertDateToString(bat.getCreditDateFin()));
    }

    public void accessibleOuNon(Boolean grise) {
        this.batimentTxtDenomination.setDisable(grise);
        this.batimentTxtAdrRue.setDisable(grise);
        this.batimentTxtAdrCP.setDisable(grise);
        this.batimentTxtAdrLocalite.setDisable(grise);
        this.batimentTxtPEB.setDisable(grise);
        this.batimentTxtSurface.setDisable(grise);
        this.batimentTxtDescription.setDisable(grise);
        this.batimentTxtLoyer.setDisable(grise);
        this.batimentTxtCharges.setDisable(grise);
        this.batimentTxtPrecompte.setDisable(grise);
        this.batimentTxtCreditBanque.setDisable(grise);
        this.batimentTxtCreditMntTot.setDisable(grise);
        this.batimentTxtCreditMensualite.setDisable(grise);
        this.batimentTxtCreditTravaux.setDisable(grise);
        this.batimentTxtCreditAchat.setDisable(grise);
        this.batimentTxtCreditDebut.setDisable(grise);
        this.batimentTxtCreditFin.setDisable(grise);
        this.batimentTxtAssuranceProprio.setDisable(grise);
        this.batimentTxtAssuranceLocataire.setDisable(grise);

    }

    private BatimentBd garnirBatiment() {
        BatimentBd l;
        if (batimentEnCours.getIdBatiment() == null) {
            l = new BatimentBd();
        } else {
            //recharger l'objet avant modif
            EntityManager em = EntityManagerHelper.EM.open();
            BatimentBsn batrimentSn = new BatimentBsn();
            l = batrimentSn.trouverBatimentParId(batimentEnCours.getIdBatiment());
            EntityManagerHelper.EM.close();
        }
        LOGGER.log(Level.INFO, "rue : {0}", batimentTxtAdrRue.getText());
        l.setAdresse(batimentTxtAdrRue.getText());
        l.setAdresseCP(batimentTxtAdrCP.getText());
        l.setAdresseLocalite(batimentTxtAdrLocalite.getText());
        l.setDenomination(batimentTxtDenomination.getText());
        //l.setIdBatiment(pEdition.getBatimentEdition().getIdBatiment());
        l.setPEB(batimentTxtPEB.getText());
        if (batimentTxtSurface.getText() != null) {
            l.setSurface(Convert.convertStringToDouble(batimentTxtSurface.getText()));
        } else {
            l.setSurface(0D);
        }
        l.setDescription(batimentTxtDescription.getText());
        l.setLoyer(0D);
        if (batimentTxtLoyer.getText() != null) {
            l.setLoyer(Convert.convertStringToDouble(batimentTxtLoyer.getText()));

        }
        l.setCharge(0D);
        if (batimentTxtCharges.getText() != null) {
            l.setCharge(Convert.convertStringToDouble(batimentTxtCharges.getText()));

        }
        l.setPrecompteImmobilier(0D);
        if (batimentTxtPrecompte.getText() != null) {
            l.setPrecompteImmobilier(Convert.convertStringToDouble(batimentTxtPrecompte.getText()));
        }

        l.setCreditBanque(batimentTxtPrecompte.getText());
        l.setCreditMontantTotal(0D);
        if (batimentTxtCreditMntTot.getText() != null) {
            l.setCreditMontantTotal(Convert.convertStringToDouble(batimentTxtCreditMntTot.getText()));
        }
        l.setCreditMontantAchat(0D);
        if (batimentTxtCreditMntTot.getText() != null) {
            l.setCreditMontantAchat(Convert.convertStringToDouble(batimentTxtCreditAchat.getText()));
        }
        l.setCreditMontantTravaux(0D);
        if (batimentTxtCreditTravaux.getText() != null) {
            l.setCreditMontantTravaux(Convert.convertStringToDouble(batimentTxtCreditTravaux.getText()));
        }
        l.setCreditMontantMensualite(0D);
        if (batimentTxtCreditTravaux.getText() != null) {
            l.setCreditMontantMensualite(Convert.convertStringToDouble(batimentTxtCreditTravaux.getText()));
        }
        l.setCreditDateDebut(Convert.convertStringToDate(batimentTxtCreditDebut.getText()));
        l.setCreditDateFin(Convert.convertStringToDate(batimentTxtCreditFin.getText()));
        l.setAssuranceProprio(batimentTxtAssuranceProprio.getText());
        l.setAssuranceLocataire(batimentTxtAssuranceLocataire.getText());

        return l;
    }

    @FXML
    private void dupliquerBatimentAction(ActionEvent event) {
    }

    @FXML
    private void enregistrerModificationBatimentAction(ActionEvent event) {
        LOGGER.log(Level.INFO, "enregistrerModificationBatimentAction");

        BatimentBd nouveauBatiment = garnirBatiment();
        BatimentBsn batrimentSn = new BatimentBsn();
        if (batimentEnCours.getIdBatiment() == null) {
            //Ajout
            LOGGER.log(Level.INFO, "ajout");

            BatimentBd nouveauBatimentBd = batrimentSn.ajouterBatiment(nouveauBatiment);
            messageInfo.setText("Batiment ajotué id : " + nouveauBatimentBd.getIdBatiment());

        } else {
            LOGGER.log(Level.INFO, "modif");
            batrimentSn.modifierBatiment(nouveauBatiment);
            //batimentEnCours=null;
            messageInfo.setText("Modifications enregistrées");
        }

    }

    @FXML
    private void rechargerBatimentAction(ActionEvent event) {
        Long id = null;
        if (batimentEnCours != null) {
            id = batimentEnCours.getIdBatiment();
        } else {
            id = 1L;
        }
        rechercherBatimentParId(id);
    }

    @FXML
    private void ajouterBatimentAction(ActionEvent event) {
    }

    @FXML
    private void supprimerBatimentAction(ActionEvent event) {
        if (batimentEnCours != null && batimentEnCours.getIdBatiment() != null) {            
            BatimentBsn batrimentSn = new BatimentBsn();
            batrimentSn.supprimerBatiment(batimentEnCours.getIdBatiment());
            batimentEnCours = null;            
        }
    }

    @FXML
    private void nouveauBatimentAction(ActionEvent event) {
        batimentEnCours = new BatimentBd();
        pEditionGarnirDetailBatiment(batimentEnCours);
    }

    @FXML
    private void voirBatimentAction(ActionEvent event) {
    }

    @FXML
    private void locationAction(ActionEvent event) throws IOException {
        if(batimentEnCours!=null && batimentEnCours.getIdBatiment()!=null){
            LocationBsn locationBsn=new LocationBsn();
            LocationBd locationEnCours = locationBsn.trouverLocationParIdBatimentDate(batimentEnCours.getIdBatiment());
            String messageInfo="";
            if(locationEnCours!=null){
                MainApp.getLocationController().setLocationEdition(locationEnCours);                
            }else{
                messageInfo="Il n'y a pas encore de location";
                LocationBd nouvelleLocation=new LocationBd();
                nouvelleLocation.setIdLocataire(0L);
                nouvelleLocation.setIdBatiment(batimentEnCours.getIdBatiment());
                MainApp.getLocationController().setLocationEdition(new LocationBd());
            }
            MainApp.getLocationController().getMessageInfo().setText(messageInfo);
        }        
        
        MainApp.getController().afficherEcran(ConstantesConseilImmo.URL_LOCATION);
    }
     @FXML
    private void locataireAction(ActionEvent event) throws IOException {
        if(batimentEnCours!=null && batimentEnCours.getIdBatiment()!=null){
           LocationBsn locationBsn=new LocationBsn();
            LocationBd locationEnCours = locationBsn.trouverLocationParIdBatimentDate(batimentEnCours.getIdBatiment());
            String messageInfoLocation="";
            String messageInfoLocataire="";
            if(locationEnCours!=null){
                LocataireBsn locataireBsn=new LocataireBsn();
            List<LocataireBd> locatairesEnCours = locataireBsn.trouverLocatairesListeParIdLocation(locationEnCours.getIdLocation());
            if(locatairesEnCours!=null && !locatairesEnCours.isEmpty()){
                MainApp.getLocataireController().setLocatairesEdition(locatairesEnCours);
            }else{
                 MainApp.getLocataireController().setLocatairesEdition(null);
                 messageInfoLocataire="Il n'y a pas encore de locataires";
            }
                MainApp.getLocataireController().getMessageInfo().setText(messageInfoLocataire);
            }else{
                messageInfoLocation="Il n'y a pas encore de location, donc pas de locataire";
                MainApp.getLocationController().setLocationEdition(new LocationBd());
            }
            MainApp.getLocationController().getMessageInfo().setText(messageInfoLocation);
        }        
        
        MainApp.getController().afficherEcran(ConstantesConseilImmo.URL_LOCATION);
    }
    
    

    @FXML
    private void testAction(ActionEvent event) throws IOException {
        BatimentBsn batrimentSn = new BatimentBsn();
        batrimentSn.test();        
    }

    @FXML
    private void btnVoirLocataireAction(ActionEvent action) throws IOException {
            
        MainApp.getController().afficherEcran(ConstantesConseilImmo.URL_LOCATAIRE);
    }

    public BatimentBd getBatimentEnCours() {
        return batimentEnCours;
    }

    public void setBatimentEnCours(BatimentBd batimentEnCours) {
        this.batimentEnCours = batimentEnCours;
    }

    public void changerDeBatiment(BatimentBd b) {
        this.setBatimentEnCours(b.createClone());
        this.pEditionGarnirDetailBatiment(b);
    }
}
